<?php
/**
 * 团队页面模板
 */

function renderTeamPage($data) {
    extract($data);
    ?>
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
        <title>我的团队</title>
        <?php echo getCssLink('assets/css/mobile.css'); ?>
        <?php echo getCssLink('assets/css/team.css'); ?>
        <link rel="icon" href="<?php echo getVersionedUrl('static/img/logo.png'); ?>" sizes="32x32" type="image/png">
    </head>
    <body>
        <div class="team-container">
            <?php renderBackButton(); ?>
            <?php renderTeamHeader(); ?>
            <?php renderTeamStats($team_stats); ?>
            <?php renderInviteSection($user, $invite_link); ?>
            <?php renderQRCodeSection($user, $username); ?>
            <?php renderTeamMembers($team_members); ?>
            <?php renderCommissionsSection($recent_commissions); ?>
        </div>
        
        <script src="<?php echo getVersionedUrl('assets/js/team.js'); ?>"></script>
    </body>
    </html>
    <?php
}

function renderBackButton() {
    ?>
    <button class="back-btn" data-action="back">← 返回</button>
    <?php
}

function renderTeamHeader() {
    ?>
    <div class="team-header">
        <h1>我的团队</h1>
        <p>邀请好友，获得手续费分成</p>
    </div>
    <?php
}

function renderTeamStats($team_stats) {
    ?>
    <div class="team-stats">
        <div class="stat-card">
            <div class="stat-value"><?php echo $team_stats['team_size']; ?></div>
            <div class="stat-label">团队成员</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">¥<?php echo number_format($team_stats['total_commission_earned'], 2); ?></div>
            <div class="stat-label">累计分成</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">¥<?php echo number_format($team_stats['total_commission_paid'], 2); ?></div>
            <div class="stat-label">已发放分成</div>
        </div>
    </div>
    <?php
}

function renderInviteSection($user, $invite_link) {
    ?>
    <div class="invite-section">
        <h3 class="section-title">邀请好友</h3>
        <p>分享您的邀请码，好友注册后您将获得其交易手续费的40%作为分成</p>
        
        <div class="invite-code">
            <div class="invite-code-text" data-invite-code><?php echo htmlspecialchars($user['invite_code'] ?? '未生成'); ?></div>
            <button class="copy-btn" data-action="copy-invite">复制邀请码</button>
        </div>
        
        <div class="invite-link-container">
            <h4>邀请链接：</h4>
            <input type="text" id="invite-link" class="invite-link-input" value="<?php echo htmlspecialchars($invite_link); ?>" readonly>
            <button class="copy-btn" data-action="copy-link">复制邀请链接</button>
        </div>
    </div>
    <?php
}

function renderQRCodeSection($user, $username) {
    $invite_code = $user['invite_code'] ?? '';
    $invite_url = "http://" . $_SERVER['HTTP_HOST'] . "/register.php?invite=" . urlencode($invite_code);
    ?>
    <div class="qr-code-section">
        <h3 class="section-title">📱 邀请二维码</h3>
        <p class="section-description">好友扫描二维码即可直接注册并建立邀请关系</p>
        
        <!-- 二维码显示区域 -->
        <div class="qr-display-area">
            <div class="qr-code-container">
                <?php renderQRCode($user, $username); ?>
            </div>
            
            <!-- 二维码信息 -->
            <div class="qr-info">
                <div class="qr-info-item">
                    <span class="qr-info-label">邀请码：</span>
                    <span class="qr-info-value" data-invite-code><?php echo htmlspecialchars($invite_code); ?></span>
                </div>
                <div class="qr-info-item">
                    <span class="qr-info-label">注册链接：</span>
                    <span class="qr-info-value qr-url" data-qr-url><?php echo htmlspecialchars($invite_url); ?></span>
                </div>
            </div>
        </div>
        
        <!-- 操作按钮区域 -->
        <div class="qr-actions">
            <div class="qr-actions-row">
                <button class="qr-btn primary" data-action="download-qr" data-tooltip="保存二维码到手机相册">
                    <span class="btn-icon">💾</span>
                    <span class="btn-text">下载二维码</span>
                </button>
                <button class="qr-btn secondary" data-action="refresh-qr" data-tooltip="重新生成二维码">
                    <span class="btn-icon">🔄</span>
                    <span class="btn-text">刷新二维码</span>
                </button>
            </div>
            <div class="qr-actions-row">
                <button class="qr-btn secondary" data-action="share-qr" data-tooltip="分享给好友">
                    <span class="btn-icon">📤</span>
                    <span class="btn-text">分享二维码</span>
                </button>
                <button class="qr-btn secondary" data-action="copy-qr-url" data-tooltip="复制注册链接">
                    <span class="btn-icon">🔗</span>
                    <span class="btn-text">复制链接</span>
                </button>
            </div>
        </div>
        
        <!-- 使用说明 -->
        <div class="usage-instructions">
            <h4 class="instructions-title">📋 使用说明</h4>
            <div class="instructions-grid">
                <div class="instruction-item">
                    <div class="instruction-icon">📱</div>
                    <div class="instruction-text">
                        <strong>保存二维码</strong>
                        <p>长按二维码保存到手机相册</p>
                    </div>
                </div>
                <div class="instruction-item">
                    <div class="instruction-icon">📤</div>
                    <div class="instruction-text">
                        <strong>分享给好友</strong>
                        <p>通过微信、QQ等社交软件分享</p>
                    </div>
                </div>
                <div class="instruction-item">
                    <div class="instruction-icon">🔍</div>
                    <div class="instruction-text">
                        <strong>好友扫码</strong>
                        <p>好友扫码后自动跳转注册页面</p>
                    </div>
                </div>
                <div class="instruction-item">
                    <div class="instruction-icon">💰</div>
                    <div class="instruction-text">
                        <strong>获得分成</strong>
                        <p>好友交易时您将获得40%手续费分成</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 二维码状态指示器 -->
        <div class="qr-status">
            <div class="status-indicator" id="qr-status-indicator">
                <span class="status-dot"></span>
                <span class="status-text">二维码已生成</span>
            </div>
        </div>
    </div>
    <?php
}

function renderQRCode($user, $username) {
    try {
        require_once __DIR__ . '/../includes/qr_generator.php';
        
        // 生成样式二维码
        $qr_data = generateStyledInviteQRCode($user['invite_code'] ?? '', $username, 300);
        if ($qr_data) {
            echo "<img id='qr-code-image' class='qr-code-image' src='{$qr_data}' alt='邀请二维码' data-username='{$username}'>";
        } else {
            // 如果样式二维码失败，使用简单二维码
            $simple_qr = generateInviteQRCode($user['invite_code'] ?? '', $username, 300);
            if ($simple_qr) {
                echo "<img id='qr-code-image' class='qr-code-image' src='{$simple_qr}' alt='邀请二维码' data-username='{$username}'>";
            } else {
                echo "<div class='error-message'>二维码生成失败</div>";
            }
        }
    } catch (Exception $e) {
        echo "<div class='error-message'>二维码生成异常: " . htmlspecialchars($e->getMessage()) . "</div>";
    }
}

function renderTeamMembers($team_members) {
    ?>
    <div class="team-members">
        <h3 class="section-title">团队成员 (<?php echo count($team_members); ?>)</h3>
        
        <?php if (empty($team_members)): ?>
            <div class="empty-state">
                <p>暂无团队成员</p>
                <p>分享您的邀请码，邀请好友加入吧！</p>
            </div>
        <?php else: ?>
            <?php foreach ($team_members as $member): ?>
                <div class="member-card">
                    <div class="member-header">
                        <div class="member-name"><?php echo $member['username']; ?></div>
                        <div class="member-join-date">加入时间：<?php echo $member['join_date']; ?></div>
                    </div>
                    
                    <div class="member-stats">
                        <div class="member-stat">
                            <div class="member-stat-value">¥<?php echo number_format($member['balance'], 2); ?></div>
                            <div class="member-stat-label">当前余额</div>
                        </div>
                        <div class="member-stat">
                            <div class="member-stat-value"><?php echo $member['transaction_count']; ?></div>
                            <div class="member-stat-label">交易次数</div>
                        </div>
                        <div class="member-stat">
                            <div class="member-stat-value">¥<?php echo number_format($member['total_trading_amount'], 2); ?></div>
                            <div class="member-stat-label">交易总额</div>
                        </div>
                        <div class="member-stat">
                            <div class="member-stat-value">¥<?php echo number_format($member['total_fees_paid'], 2); ?></div>
                            <div class="member-stat-label">手续费总额</div>
                        </div>
                        <div class="member-stat">
                            <div class="member-stat-value">¥<?php echo number_format($member['commission_earned'], 2); ?></div>
                            <div class="member-stat-label">为您赚取</div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
    <?php
}

function renderCommissionsSection($recent_commissions) {
    ?>
    <div class="commissions-section">
        <h3 class="section-title">最近分成记录</h3>
        
        <?php if (empty($recent_commissions)): ?>
            <div class="empty-state">
                <p>暂无分成记录</p>
                <p>团队成员交易后，您将获得手续费分成</p>
            </div>
        <?php else: ?>
            <?php foreach ($recent_commissions as $commission): ?>
                <div class="commission-item">
                    <div class="commission-header">
                        <div class="commission-user"><?php echo $commission['user_name']; ?></div>
                        <div class="commission-amount">+¥<?php echo number_format($commission['commission_amount'], 2); ?></div>
                    </div>
                    <div class="commission-details">
                        交易金额：¥<?php echo number_format($commission['transaction_amount'], 2); ?> | 
                        手续费：¥<?php echo number_format($commission['original_fee'], 2); ?> | 
                        分成比例：<?php echo number_format($commission['commission_rate'] * 100, 1); ?>% | 
                        时间：<?php echo $commission['formatted_date']; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
    <?php
}
?>
